package com.study.demo.widget;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by sll on 15/4/27.
 */
public class WrapperListPreference extends ListPreference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WrapperListPreference(Context context, AttributeSet attrs, int defStyleAttr,
                                 int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WrapperListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public WrapperListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private AlertDialog.Builder mBuilder;

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);

        mBuilder = builder;
    }

    private String getFieldValue(String filed) throws Exception {
        Field titleField =
                WrapperListPreference.class.getSuperclass().getSuperclass().getDeclaredField(filed);
        titleField.setAccessible(true);
        return titleField.get(this).toString();
    }

    @Override
    protected void showDialog(Bundle state) {
        try {
            if (Build.VERSION.SDK_INT < 100) {
                CharSequence[] mEntries = getEntries();
                String[] mEntriesStrArr = new String[mEntries.length];
                for (int i = 0; i < mEntries.length; i++)
                    mEntriesStrArr[i] = mEntries[i].toString();

                Method method =
                        WrapperListPreference.class.getSuperclass().getDeclaredMethod("getValueIndex");
                method.setAccessible(true);
                int mClickedDialogEntryIndex = Integer.parseInt(method.invoke(this).toString());

                String dialogTitle = getFieldValue("mDialogTitle");
                String mNegativeButtonText = getFieldValue("mNegativeButtonText");

                new MaterialDialog.Builder(getContext()).title(dialogTitle)
                        .items(mEntriesStrArr)
                        .itemsCallbackSingleChoice(mClickedDialogEntryIndex,
                                this::onSelection)
                        .dismissListener(WrapperListPreference.this)
                        .negativeText(mNegativeButtonText)
                        .onNegative(this::onClick)
                        .show();
            } else {
                super.showDialog(state);
            }
        } catch (Exception e) {
            super.showDialog(state);
        }
    }

    private boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
        try {
            Field titleField = WrapperListPreference.class.getSuperclass()
                    .getDeclaredField("mClickedDialogEntryIndex");
            titleField.setAccessible(true);
            titleField.set(WrapperListPreference.this, which);
        } catch (Exception e) {
        }

        WrapperListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
        dialog.dismiss();
        return true;
    }

    private void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
        dialog.dismiss();
    }
}
